<template>
  <div class="app-container ">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['facilities:atmosphere:add']"
          icon="el-icon-plus"
          plain
          size="small"
          type="primary"
          @click="handleAdd"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['facilities:atmosphere:edit']"
          :disabled="single"
          icon="el-icon-edit"
          plain
          size="small"
          type="success"
          @click="handleUpdate"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['facilities:atmosphere:remove']"
          :disabled="multiple"
          icon="el-icon-delete"
          plain
          size="small"
          type="danger"
          @click="handleDelete"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['facilities:atmosphere:export']"
          :loading="exportLoading"
          icon="el-icon-download"
          plain
          size="small"
          type="warning"
          @click="handleExport"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="atmosphereList" @selection-change="handleSelectionChange">
      <el-table-column align="center" type="selection" width="55"/>
      <el-table-column align="center" label="设施名称" prop="etfName"/>
      <el-table-column :formatter="typeFormat" align="center" label="治理设施类型" prop="etfType"/>
      <el-table-column align="center" label="治理设施工艺" prop="etfCraft"/>
      <!--      <el-table-column align="center" label="用电量" prop="etfEnergyConsumption"/>-->
      <!--      <el-table-column align="center" label="药剂用量" prop="etfMedicamentDosage"/>-->
      <!--      <el-table-column align="center" label="污染物进入量" prop="etfWaster"/>-->
      <!--      <el-table-column align="center" label="处理量" prop="etfProcess"/>-->
      <!--      <el-table-column align="center" label="处理后的排放量" prop="etfProcessEmission"/>-->
      <!--      <el-table-column align="center" label="处理后主要污染物的浓度" prop="etfProcessPC"/>-->
      <!--      <el-table-column align="center" label="新产生的污染物种类" prop="etfNewMasterPType"/>-->
      <!--      <el-table-column align="center" label="数量" prop="etfNewQuantity"/>-->
      <el-table-column align="center" label="年运行(小时)" prop="etfYearTime"/>
      <el-table-column align="center" label="年运行费用(万元)" prop="etfYearMoney"/>
      <el-table-column align="center" label="投入使用日期" prop="etfNewUseTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.etfNewUseTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <!--      <el-table-column align="center" label="环境监察记录" prop="etfEnvRecord"/>-->
      <!--      <el-table-column align="center" label="运行规程" prop="etfRunRule"/>-->
      <el-table-column align="center" label="设施平面布置图" prop="etfFacilityPicture">
        <template slot-scope="scope" v-if="scope.row.etfFacilityPicture">
          <a :href="`${baseUrl}${scope.row.etfFacilityPicture}`" target="_blank"
             class="buttonText">{{
              scope.row.etfFacilityPicture.toString().substring(scope.row.etfFacilityPicture.lastIndexOf("/") + 1)
            }}</a>
        </template>
      </el-table-column>
      <!--      <el-table-column align="center" label="运行记录" prop="etfRunRecord"/>-->
      <!--      <el-table-column align="center" label="备注" prop="remark"/>-->
      <el-table-column align="center" class-name="small-padding fixed-width" label="操作">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['facilities:atmosphere:edit']"
            size="small"
            @click="handleUpdate(scope.row,scope.$index)"
          >修改
          </el-button>
          <el-button
            v-hasPermi="['facilities:atmosphere:remove']"
            size="small"
            @click="handleDelete(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :limit.sync="queryParams.pageSize"
      :page.sync="queryParams.pageNum"
      :total="total"
      @pagination="getList"
    />

    <!-- 添加或修改废气治理设施对话框 -->
    <el-dialog :title="title" :visible.sync="open" append-to-body class="app-container-atmosphere" width="70%">
      <el-form ref="form" :model="form" :rules="rules" class="clearfix" label-width="140px">
        <div class="content-left" style="margin-left: 4%">
          <div class="title2"><span> </span>基本信息</div>
          <el-form-item label="设施名称" prop="etfName">
            <el-input v-model="form.etfName" placeholder="请输入设施名称"/>
          </el-form-item>
          <el-form-item label="设施编号" prop="etfCode">
            <el-input v-model="form.etfCode" placeholder="请输入设施编号"/>
          </el-form-item>
          <el-form-item label="设施型号" prop="etfModel">
            <el-input v-model="form.etfModel" placeholder="请输入设施型号"/>
          </el-form-item>
          <el-form-item label="治理设施类型" prop="etfType">
            <el-select v-model="form.etfType" placeholder="请选择" filterable allow-create >
              <el-option
                v-for="dict in typeOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="治理设施工艺" prop="etfCraft">
            <el-input v-model="form.etfCraft" placeholder="请输入治理设施工艺"/>
          </el-form-item>
<!--          <el-form-item label="污染物进入量(吨)" prop="etfWaster">-->
<!--            <el-input v-model="form.etfWaster" placeholder="请输入污染物进入量"/>-->
<!--          </el-form-item>-->
<!--          <el-form-item label="年处理量(千克)" prop="etfProcess">
            <el-input v-model="form.etfProcess" placeholder="请输入处理量"/>
          </el-form-item>-->

        </div>
        <div class="content-middle">
          <div class="title2"><span> </span>处理信息</div>
<!--          <el-form-item label="处理后污染物浓度" prop="etfProcessPC">
            <el-input v-model="form.etfProcessPC" placeholder="请输入处理后污染物浓度"/>
          </el-form-item>-->
<!--          <el-form-item label="新生污染物种类" prop="etfNewMasterPType">
            <el-input v-model="form.etfNewMasterPType" placeholder="请输入新生污染物种类"/>
          </el-form-item>
          <el-form-item label="新生污染物数量" prop="etfNewQuantity">
            <el-input v-model="form.etfNewQuantity" placeholder="请输入新生污染物数量"/>
          </el-form-item>-->

          <el-form-item label="用电量(千瓦)" prop="etfEnergyConsumption">
            <el-input v-model="form.etfEnergyConsumption" placeholder="请输入用电量"/>
          </el-form-item>
          <el-form-item label="药剂用量(吨)" prop="etfMedicamentDosage">
            <el-input v-model="form.etfMedicamentDosage" placeholder="请输入药剂用量"/>
          </el-form-item>

          <el-form-item label="年运行(小时)" prop="etfYearTime" type="number">
            <el-input v-model="form.etfYearTime" placeholder="请输入年运行(小时)"/>
          </el-form-item>
          <el-form-item label="年运行费用(万元)" prop="etfYearMoney">
            <el-input v-model="form.etfYearMoney" placeholder="请输入年运行费用(万元)"/>
          </el-form-item>
          <el-form-item label="投入使用日期" prop="etfNewUseTime">
            <el-date-picker v-model="form.etfNewUseTime" clearable
                            placeholder="选择投入使用日期"
                            size="small"
                            style="width:100%"
                            type="date"
                            value-format="yyyy-MM-dd">
            </el-date-picker>
          </el-form-item>
<!--          <el-form-item label="处理后的排放量" prop="etfProcessEmission">
            <el-input v-model="form.etfProcessEmission" placeholder="请输入处理后的排放量"/>
          </el-form-item>-->
        </div>
        <div class="content-right">
          <div class="title2"></div>
          <el-form-item label="环境监察记录" prop="etfEnvRecord">
            <FileUpload v-model="form.etfEnvRecord"/>
          </el-form-item>
          <el-form-item label="运行规程制度" prop="etfRunRule">
            <FileUpload v-model="form.etfRunRule"/>
          </el-form-item>
          <el-form-item label="设施平面布置图" prop="etfFacilityPicture">
            <ImageUpload v-model="form.etfFacilityPicture"/>
          </el-form-item>
          <el-form-item label="运行记录" prop="etfRunRecord">
         <FileUpload v-model="form.etfRunRecord" />
          </el-form-item>
        </div>
      </el-form>

       <div class="more-title">污染物详细信息</div>
       <div  style="display: flex; align-items: center"  v-for="(item,index) in itemPerson">
          <div style="width: 80%; padding-left: 30px;" >
            <div   class="fuliao "  >
              <div class="ful-input" style="justify-content: flex-start;">
                <div class="ful-input-box">
                  <span style="width: 79%">主要污染物种类 </span>
                  <el-input  style="height: 30px;" v-model="item.x1"></el-input>
                </div>
              </div>
            </div>
            <div   class="fuliao "style="border: none"  >
              <div class="ful-input">
                <div class="ful-input-box">
                  <span>年排放量(吨)</span>
                  <el-input  style="height: 30px;"  v-model="item.x2"></el-input>
                </div>
                <div class="ful-input-box">
                  <span>处理前浓度(千克)</span>
                  <el-input  style="height: 30px;"  v-model="item.x3"></el-input>
                </div>
                <div class="ful-input-box">
                  <span>处理后浓度(千克)</span>
                  <el-input  style="height: 30px;"  v-model="item.x4"></el-input>
                </div>
              </div>

            </div>
            <div   class="fuliao "  style="border: none">
              <div class="ful-input">
                <div class="ful-input-box">
                  <span>污染治理设施 </span>
                  <el-input  style="height: 30px;"  v-model="item.x5"></el-input>
                </div>
                <div class="ful-input-box">
                  <span>排口名称</span>
                  <el-input  style="height: 30px;"  v-model="item.x6"></el-input>
                </div>
                <div class="ful-input-box">
                  <span>年处理量(吨)</span>
                  <el-input  style="height: 30px;"  v-model="item.x7"></el-input>
                </div>
              </div>
            </div>
          </div>
         <div  class="ful-btn">
           <el-button type="primary" @click="addMore(index)" style="height: 40px; line-height: 40px;padding: 0;padding-left: 20px; padding-right: 20px">添加</el-button>
           <el-button type="danger" @click="removeMore(index)"  style="height: 40px; line-height: 40px;padding: 0;padding-left: 20px; padding-right: 20px">删除</el-button>
         </div>
       </div>








      <div slot="footer" class="dialog-footer tianjie_button" style="text-align: center; margin-top: 50px">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  addAtmosphere,
  delAtmosphere,
  exportAtmosphere,
  getAtmosphere,
  listAtmosphere,
  updateAtmosphere
} from "@/api/facilities/atmosphere";
import {getToken} from "@/utils/auth";
import {InfoItem} from "@/api/enterprise/info";
import {isNotMUstTwoPoint,validatePhoneTwo,validateIdNo,validateEMail} from "@/api/validate/validate";


export default {
  name: "Atmosphere",
  data() {
    //脱硫、脱硝、除尘、油烟净化
    return {
      itemPerson:[
        {
          x1:"",
          x2:"",
          x3:"",
          x4:"",
          x5:"",
          x6:"",
          x7:"",
        }
      ],
      baseUrl: process.env.VUE_APP_BASE_API,
      etfIndex: '0',
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 废气治理设施表格数据
      atmosphereList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        etfName: null,
        etfCode: null,
        etfModel: null,
        etfType: null,
        etfCraft: null,
        etfEnergyConsumption: null,
        etfMedicamentDosage: null,
        etfWaster: null,
        etfProcess: null,
        etfProcessEmission: null,
        etfProcessPC: null,
        etfNewMasterPType: null,
        etfNewQuantity: null,
        etfExhaustionHoleName: null,
        etfYearTime: null,
        etfYearMoney: null,
        etfNewUseTime: null,
        etfEnvRecord: null,
        etfRunRule: null,
        etfFacilityPicture: null,
        etfRunRecord: null,
        epId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        etfName: [
          {required: true, message: "设施名称不能为空", trigger: "blur"}
        ],
        etfEnergyConsumption: [
          {validator:isNotMUstTwoPoint, trigger: "blur"}
        ],
        etfMedicamentDosage: [
          {validator:isNotMUstTwoPoint, trigger: "blur"}
        ],
        etfYearMoney: [
          {validator:isNotMUstTwoPoint, trigger: "blur"}
        ],
        etfWaster: [
          {validator:isNotMUstTwoPoint, trigger: "blur"}
        ],
        etfProcess: [
          {validator:isNotMUstTwoPoint, trigger: "blur"}
        ],
        etfProcessPC: [
          {validator:isNotMUstTwoPoint, trigger: "blur"}
        ],
        etfNewQuantity: [
          {validator:isNotMUstTwoPoint, trigger: "blur"}
        ],
      },
      typeOptions:[],
    };
  },
  created() {
    this.getList();
    this.getDicts("fqzlsslx").then(response => {
      this.typeOptions = response.data;
    });
  },
  methods:{
    btnMore(){
      var arrx1=[];
      var arrx2=[];
      var arrx3=[];
      var arrx4=[];
      var arrx5=[];
      var arrx6=[];
      var arrx7=[];
     var stautus;
      this.itemPerson.map(function(item){
        if(item.x1 && item.x2 && item.x3 && item.x4 && item.x5 && item.x6 && item.x7){
          arrx1.push(item.x1);
          arrx2.push(item.x2);
          arrx3.push(item.x3);
          arrx4.push(item.x4);
          arrx5.push(item.x5);
          arrx6.push(item.x6);
          arrx7.push(item.x7);
          stautus=true;
        }else{
          stautus=false;
        }
      })
      this.form.etfNewMasterPType=arrx1.join(',') //原来的新生污染物种类字段替换成主要污染物种类
      this.form.etfProcess=arrx2.join(','); //年排放量
      this.form.etfNewQuantity=arrx3.join(','); //原来的新生污染物数量 改为 处理前浓度
      this.form.etfProcessPC=arrx4.join(','); //处理后浓度
      this.form.etfWaster=arrx5.join(','); //污染治理设施 原来的污染物进入量
      this.form.etfExhaustionHoleName=arrx6.join(','); //排口名称
      this.form.etfProcessEmission=arrx7.join(','); //年处理量
      return stautus;
    },
    addMore(index){
      this.itemPerson.push( {
        x1:"",
        x2:"",
        x3:"",
        x4:"",
        x5:"",
        x6:"",
        x7:"",
      })
    },
    removeMore(index){
    if( this.itemPerson.length<=1){
        this.msgError("至少保留一行");
        return;
      }
      this.itemPerson.splice(index, 1)
    },
    selectBlur(e){
      this.form.etfType = 5;
    },
    typeFormat(row, column) {
      return this.selectDictLabel(this.typeOptions, row.etfType);
    },
    /** 查询废气治理设施列表 */
    getList() {
      this.loading = true;
      const username = this.$store.state.user.name;
      InfoItem(username).then(res => {
        if (res.hasOwnProperty("data")) {
          this.queryParams.epId = res.data.epId;
          listAtmosphere(this.queryParams).then(response => {
            this.atmosphereList = response.rows;
            this.total = response.total;
            this.loading = false;
          });
        }
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        etfId: null,
        etfName: null,
        etfCode: null,
        etfModel: null,
        etfType: null,
        etfCraft: null,
        etfEnergyConsumption: null,
        etfMedicamentDosage: null,
        etfWaster: null,
        etfProcess: null,
        etfProcessEmission: null,
        etfProcessPC: null,
        etfNewMasterPType: null,
        etfNewQuantity: null,
        etfExhaustionHoleName: null,
        etfYearTime: null,
        etfYearMoney: null,
        etfNewUseTime: null,
        etfEnvRecord: null,
        etfRunRule: null,
        etfFacilityPicture: null,
        etfRunRecord: null,
        epId: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.etfId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加废气治理设施";
    },
    /** 修改按钮操作 */
    handleUpdate(row,index) {
      this.reset();
      const etfId = row.etfId || this.ids
      getAtmosphere(etfId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改废气治理设施";
        var arr1=this.form.etfNewMasterPType.split(',')
        var arr2= this.form.etfProcess.split(',')
        var arr3= this.form.etfNewQuantity.split(',')
        var arr4= this.form.etfProcessPC.split(',')
        var arr5= this.form.etfWaster.split(',')
        var arr6= this.form.etfExhaustionHoleName.split(',')
        var arr7= this.form.etfProcessEmission.split(',')
        var arrAll=[];
            for(var i=0;i<arr1.length;i++){
              arrAll.push(
                {
                  x1:arr1[i],
                  x2:arr2[i],
                  x3:arr3[i],
                  x4:arr4[i],
                  x5:arr5[i],
                  x6:arr6[i],
                  x7:arr7[i],
                }
              )
            }
        this.itemPerson=arrAll;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.etfId != null) {
            if(!this.btnMore()) {
              this.msgError("请填写完整的信息");
            }else{
              updateAtmosphere(this.form).then(response => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            }
          } else {
            const username = this.$store.state.user.name;
            InfoItem(username).then(res => {
              if (res.hasOwnProperty("data")) {
                this.form.epId = res.data.epId;
              }
              if(!this.btnMore()){
                this.msgError("请填写完整的信息");
              }else{
                addAtmosphere(this.form).then(response => {
                  this.msgSuccess("新增成功");
                  this.open = false;
                  this.getList();
                });
              }
            })
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const etfIds = row.etfId || this.ids;
      this.$confirm('是否确认删除废气治理设施编号为"' + etfIds + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return delAtmosphere(etfIds);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有废气治理设施数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.exportLoading = true;
        return exportAtmosphere(queryParams);
      }).then(response => {
        this.download(response.msg);
        this.exportLoading = false;
      }).catch(() => {
      });
    }
  }
};
</script>
<style scoped>
.app-container-atmosphere {
  padding-left: 60px;
  padding-right: 60px;
}
/deep/ .el-upload--picture-card{
  height: 120px;
}
/deep/ .el-upload-list--picture-card .el-upload-list__item{
  margin: 0;
  margin-left: 20px;
  height: 107px;
}
.more-title{
  text-align: center;
  height: 40px;
  line-height: 40px;
  color: #333;
  font-weight: bold;
  border-bottom: 1px solid #ccc;
}
.fuliao{
  width: 100%;
  height: 30px;
  padding-left: 46px;
  font-size: 12px;
  margin: 0 auto;
  /*border-top: 1px solid #ccc;*/
  margin-bottom: 10px;
  margin-top: 14px;
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  /*padding-top: 32px;*/
  -webkit-box-align: center;
  -ms-flex-align: center;
  align-items: center;
  justify-content: space-between;
}
.ful-input{
  width: 100%;
  display: flex;

  align-items: center;
  /*justify-content: space-around;*/
}
.ful-title{
  font-size:16px;
  color: #333;
  width: 50%;
  text-align: center;
  font-weight: 700;
}
.fuliao/deep/.el-input__inner{
  width: 100%;
  height: 30px !important;
  margin-left: 10px;
}
.content-left {
  width: 30% !important;
  float: left;
}
.ful-btn{
  width: 20%;
  margin-left: 10px;
}
.ful-input-box{
  width: 28%;
  font-weight: bold;

  display: flex;
  align-items: center;
  justify-content: space-between;
}
.ful-input-box span{
  text-align: right;
  width: 80%;
  color: #333;
  display: inline-block;
}
.fuliao/deep/.el-input__inner{
  width: 100%;
  height: 30px !important;
}
</style>
